﻿using System;
using System.Collections.Generic;
using System.Text;
using com.huigo.Framework.ServiceUtils;

namespace com.huigo.Admin.Web.product
{
    public partial class ProductList : BasePage
    {
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }

        private string queryProduct
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["pro"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["pro"]);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void BindData()
        {
            productName.Text = queryProduct;
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("proName", queryProduct));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_PRODUCT_LIST, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<table>");
                sb.AppendLine("<thead>");
                sb.AppendLine("<tr>");
                sb.AppendLine("<th style=\"width: 20%\">");
                sb.AppendLine("产品名称");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 15%\">");
                sb.AppendLine("产品简称");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 10%\">");
                sb.AppendLine("产品种类");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 10%\">");
                sb.AppendLine("保险公司");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 20%\">");
                sb.AppendLine("供应商");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 10%\">");
                sb.AppendLine("价格");
                sb.AppendLine("</th>");
                sb.AppendLine("<th style=\"width: 10%\">");
                sb.AppendLine("结算价格");
                sb.AppendLine("</th>");
                sb.AppendLine("<th>");
                sb.AppendLine("操作");
                sb.AppendLine("</th>");
                sb.AppendLine("</tr>");
                sb.AppendLine("</thead>");
                sb.AppendLine("<tbody>");

                for (int i = 0; i < parser.RowsCount; i++)
                {
                    sb.AppendLine("<tr>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["ProductName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["ProductShortName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["TypeName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["CompanyShortName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(parser["SupName", i]);
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["ProductPrice", i])));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["ProductSettlePrice", i])));
                    sb.AppendLine("</td>");
                    sb.AppendLine("<td>");
                    sb.AppendLine("<a href=\"AddProduct.aspx?pid=" + parser["ProductId", i] + "&keepThis=true&amp;TB_iframe=true&amp;height=500&amp;width=850\" title=\"修改产品-" + parser["ProductName", i] + "\" class=\"thickbox\">修改</a>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");
                }
                sb.AppendLine("</tbody>");
                sb.AppendLine("</table>");
                litPro.Text = sb.ToString();
            }
            else
                litPro.Text = string.Empty;


        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductList.aspx?pro=" + productName.Text);
        }

        private string GetPagerUrl()
        {
            string url = "ProductList.aspx?";
            url += "page={0}";
            if (queryProduct != string.Empty)
                url += "&pro=" + queryProduct;
            return url;
        }

    }
}
